package com.zch.jh.web.rest;

import com.zch.jh.service.LogisticService;
import com.zch.jh.web.rest.errors.BadRequestAlertException;
import com.zch.jh.service.dto.LogisticDTO;

import io.github.jhipster.web.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing {@link com.zch.jh.domain.Logistic}.
 */
@RestController
@RequestMapping("/api")
public class LogisticResource {

    private final Logger log = LoggerFactory.getLogger(LogisticResource.class);

    private static final String ENTITY_NAME = "logistic";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;

    private final LogisticService logisticService;

    public LogisticResource(LogisticService logisticService) {
        this.logisticService = logisticService;
    }

    /**
     * {@code POST  /logistics} : Create a new logistic.
     *
     * @param logisticDTO the logisticDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new logisticDTO, or with status {@code 400 (Bad Request)} if the logistic has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/logistics")
    public ResponseEntity<LogisticDTO> createLogistic(@RequestBody LogisticDTO logisticDTO) throws URISyntaxException {
        log.debug("REST request to save Logistic : {}", logisticDTO);
        if (logisticDTO.getId() != null) {
            throw new BadRequestAlertException("A new logistic cannot already have an ID", ENTITY_NAME, "idexists");
        }
        LogisticDTO result = logisticService.save(logisticDTO);
        return ResponseEntity.created(new URI("/api/logistics/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * {@code PUT  /logistics} : Updates an existing logistic.
     *
     * @param logisticDTO the logisticDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated logisticDTO,
     * or with status {@code 400 (Bad Request)} if the logisticDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the logisticDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/logistics")
    public ResponseEntity<LogisticDTO> updateLogistic(@RequestBody LogisticDTO logisticDTO) throws URISyntaxException {
        log.debug("REST request to update Logistic : {}", logisticDTO);
        if (logisticDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        LogisticDTO result = logisticService.save(logisticDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, logisticDTO.getId().toString()))
            .body(result);
    }

    /**
     * {@code GET  /logistics} : get all the logistics.
     *
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of logistics in body.
     */
    @GetMapping("/logistics")
    public List<LogisticDTO> getAllLogistics() {
        log.debug("REST request to get all Logistics");
        return logisticService.findAll();
    }

    /**
     * {@code GET  /logistics/:id} : get the "id" logistic.
     *
     * @param id the id of the logisticDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the logisticDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/logistics/{id}")
    public ResponseEntity<LogisticDTO> getLogistic(@PathVariable Long id) {
        log.debug("REST request to get Logistic : {}", id);
        Optional<LogisticDTO> logisticDTO = logisticService.findOne(id);
        return ResponseUtil.wrapOrNotFound(logisticDTO);
    }

    /**
     * {@code DELETE  /logistics/:id} : delete the "id" logistic.
     *
     * @param id the id of the logisticDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/logistics/{id}")
    public ResponseEntity<Void> deleteLogistic(@PathVariable Long id) {
        log.debug("REST request to delete Logistic : {}", id);
        logisticService.delete(id);
        return ResponseEntity.noContent().headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString())).build();
    }
}
